import { ElNotification } from 'element-plus';
import config from '../config/config';
import Res from './res';
class UniPcHttp {
	/**
	 * @class UniPcHttp
	 * @method get
	 * @param {string} api - 要传入的接口地址
	 * @param {string} data - 要传入参数
	 * @return {Promise} 返回Promise对象
	 * @description uniapp.request的get方法
	 * @example 查看README.md
	 *
	 */
	get(api: string, data: any): Promise<Res> {
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.baseUrl + api,
				data: data,
				header: {
					'content-type': 'application/json',
					Authorization: uni.getStorageSync('token')
				},
				timeout: config.timeout,
				success(res: any) {
					if (res.statusCode === 200) {
						resolve(res.data);
					} else if (res.statusCode === 401) {
						// 登录过期或者无权限
						uni.clearStorageSync();
						uni.reLaunch({
							url: config.loginPage
						});
					} else if (res.statusCode === 404) {
						ElNotification({
							title: '提示',
							type: 'error',
							message: '访问地址不存在'
						});
					} else if (res.statusCode >= 500) {
						ElNotification({
							title: '提示',
							type: 'error',
							message: '服务器错误'
						});
					}
				},
				fail(err) {
					//请求失败 timeout 或者无网络等 域名错误等
					reject(err);
					ElNotification({
						title: '提示',
						type: 'error',
						message: '无法连接到服务器'
					});
				}
			});
		});
	}

	post(api: string, data: any): Promise<Res> {
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.baseUrl + api,
				method: 'POST',
				data: data,
				header: {
					'content-type': 'application/json',
					Authorization: uni.getStorageSync('token')
				},
				timeout: config.timeout,
				success(res: any) {
					if (res.statusCode === 200) {
						resolve(res.data);
					} else if (res.statusCode === 401) {
						// 登录过期或者无权限
						uni.clearStorageSync();
						uni.reLaunch({
							url: config.loginPage
						});
					} else if (res.statusCode === 404) {
						ElNotification({
							title: '提示',
							type: 'error',
							message: '访问地址不存在'
						});
					} else if (res.statusCode >= 500) {
						ElNotification({
							title: '提示',
							type: 'error',
							message: '服务器错误'
						});
					}
				},
				fail(err) {
					//请求失败 timeout 或者无网络等 域名错误等
					reject(err);
					ElNotification({
						title: '提示',
						type: 'error',
						message: '无法连接到服务器'
					});
				}
			});
		});
	}
	postForm(api: string, data: any): Promise<Res> {
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.baseUrl + api,
				method: 'POST',
				data: data,
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					Authorization: uni.getStorageSync('token')
				},
				timeout: config.timeout,
				success(res: any) {
					if (res.statusCode === 200) {
						resolve(res.data);
					} else if (res.statusCode === 401) {
						// 登录过期或者无权限
						uni.clearStorageSync();
						uni.reLaunch({
							url: config.loginPage
						});
					} else if (res.statusCode === 404) {
						ElNotification({
							title: '提示',
							type: 'error',
							message: '访问地址不存在'
						});
					} else if (res.statusCode >= 500) {
						ElNotification({
							title: '提示',
							type: 'error',
							message: '服务器错误'
						});
					}
				},
				fail(err) {
					//请求失败 timeout 或者无网络等 域名错误等
					reject(err);
					ElNotification({
						title: '提示',
						type: 'error',
						message: '无法连接到服务器'
					});
				}
			});
		});
	}
}

export default new UniPcHttp();
